<template>
  <div class="header-container">
    <div class="my-title">
      <span>MBlog后台管理系统</span>
      <div class="user-info">
        <el-menu :router="true" active-text-color="#5FB878" menu-trigger="click" mode="horizontal">
          <template>
            <el-submenu index>
              <template slot="title">
                <img :src="user.avatar||defaultAvatar" class="me-header-picture" />
              </template>
              <el-menu-item @click="logout" index>
                <i class="el-icon-back"></i>退出
              </el-menu-item>
            </el-submenu>
          </template>
        </el-menu>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  user: {
    nickname: '',
    avatar: ''
  },
  computed: {
    user () {
      let login = this.$store.state.account.length !== 0
      let avatar = this.$store.state.avatar
      return {
        login,
        avatar
      }
    }
  },
  methods: {
    logout () {
      let that = this
      this.$store
        .dispatch('logout')
        .then(() => {
          this.$router.push({ path: '/' })
        })
        .catch(error => {
          if (error !== 'error') {
            that.$message({ message: error, type: 'error', showClose: true })
          }
        })
    }
  }
}
</script>
<style>
.header-container {
  height: 100%;
  background-color: #545c64;
  padding: 0 20px;
}
.my-title {
  line-height: 60px;
  font-size: 25px;
  font-weight: 700;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.user-info {
  line-height: 60px;
  position: absolute;
  right: 180px;
  top: 0;
  background-color: #000;
}
.me-header-picture {
  line-height: 60px;
  width: 30px;
  height: 30px;
  background-color: #000;
}
</style>
